﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CueMounter {
	public class WaveHeader {
		public byte[] Data;
		public int DataBytes, Channels, SampleRate, BitsPerSample;

		public WaveHeader(int dataBytes, int channels, int sampleRate, int bitsPerSample) {
			DataBytes = dataBytes;
			Channels = channels;
			SampleRate = sampleRate;
			BitsPerSample = bitsPerSample;

			Data = new byte[44];
			Copy(Encoding.ASCII.GetBytes("RIFF"), 0);
			Copy(BitConverter.GetBytes(36 + DataBytes), 4);
			Copy(Encoding.ASCII.GetBytes("WAVE"), 8);
			Copy(Encoding.ASCII.GetBytes("fmt "), 12);
			Copy(BitConverter.GetBytes(16), 16);
			Copy(BitConverter.GetBytes((short)1), 20);
			Copy(BitConverter.GetBytes((short)Channels), 22);
			Copy(BitConverter.GetBytes(SampleRate), 24);
			Copy(BitConverter.GetBytes(SampleRate * Channels * BitsPerSample / 8), 28);
			Copy(BitConverter.GetBytes((short)(Channels * BitsPerSample / 8)), 32);
			Copy(BitConverter.GetBytes((short)BitsPerSample), 34);
			Copy(Encoding.ASCII.GetBytes("data"), 36);
			Copy(BitConverter.GetBytes(DataBytes), 40);
		}

		private void Copy(byte[] source, int targetIndex) {
			Array.Copy(source, 0, Data, targetIndex, source.Length);
		}
	}
}
